package com.example.bysj_sb.dao;

import com.example.bysj_sb.domain.GraduateProjectCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("graduateProjectCategoryDao")
public class GraduateProjectCategoryDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加
    public boolean add(GraduateProjectCategory graduateProjectCategory) {
        //sql语句
        String sql = "INSERT INTO GraduateProjectCategory (no,description,remarks) VALUES (?,?,?)";
        //执行sql语句
        int affectedRowNum = jdbcTemplate.update(sql, graduateProjectCategory.getNo(),
                graduateProjectCategory.getDescription(),graduateProjectCategory.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    //根据id查询
    public GraduateProjectCategory find(int id){
        String sql = "SELECT * FROM GraduateProjectCategory WHERE id = ?";
        //查询表的结果与实体类属性一致，可用BeanPropertyRowMapper直接生成相应对象
        List<GraduateProjectCategory> graduateProjectCategories = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(GraduateProjectCategory.class));
        if(!graduateProjectCategories.isEmpty()){
            return graduateProjectCategories.get(0);
        } else
            return null;
    }
    public List<GraduateProjectCategory> findAll(){
        String sql = "SELECT * FROM GraduateProjectCategory";
        //查询表的结果与实体类属性一致，可用BeanPropertyRowMapper直接生成相应对象
        List<GraduateProjectCategory> graduateProjectCategories =
                jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(GraduateProjectCategory.class){

                });
        return graduateProjectCategories;
    }
    public boolean update(GraduateProjectCategory graduateProjectCategory){
        //sql语句
        String sql = "UPDATE GraduateProjectCategory SET no = ? ,description=? WHERE id = ?";
        //执行sql语句
        int affectedRowNum = jdbcTemplate.update(sql,graduateProjectCategory.getNo(),
                graduateProjectCategory.getDescription(),graduateProjectCategory.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    //根据id删除
    public boolean delete(int id){
        String sql = "DELETE FROM GraduateProjectCategory WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}
